{ INFORMATICS.MCCME.RU
  Problem statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=2787
  Verdict: Accepted
}
uses SysUtils;
 
const 
	bad:int64=-round(1e12);
 
type	
	ar=array of longint;
 
var
	q,qq,k,kk,aa,bb,cc,j,pra,prb,cq:longint;
	a:array[0..8000010] of longint;
	b:array[0..2000010] of longint;
	pp:array[0..500010] of ar;
	s,t:string;
	was,used:array[0..500010] of boolean;
	first:array[0..500010] of longint;
	query:array[0..500010,1..2] of longint;
 
procedure swap(var a,b:longint);
var
	t:longint;
begin
t:=a;
a:=b;
b:=t;
end;
 
function max(x,y:longint):longint;
begin
if b[x]<b[y] then result:=x else result:=y;
end;
 
function build(n:longint):longint; // returns number of elements in big array + builds array																	 
var                                // B - input data; A - RMQ array
	k,kk,i,j:longint;
 
begin
k:=1;
while k<=n do k:=k shl 1;
kk:=k;
k:=k shl 1;
b[0]:=bad;
for i:=n+1 to kk do 
	b[i]:=bad;
j:=1;
for i:=kk to k do begin
	a[i]:=j;
	inc(j);
end;
for i:=kk-1 downto 1 do
	a[i]:=max(a[i*2],a[i*2+1]);
result:=k;
end;
 
function rmq(l,r:longint):longint; // A - RMQ array; K - number of elements in A
var
	kk:longint;
begin
kk:=k shr 1;
inc(l,kk-1);
inc(r,kk-1);
result:=max(a[l],a[r]);
while r-l>1 do begin 
	if l mod 2=0 then result:=max(result,a[l+1]);
	if r mod 2=1 then result:=max(result,a[r-1]);
	l:=l div 2;
	r:=r div 2;	
end;
end;
 
procedure mark(v:longint);
begin
inc(cc);
b[cc]:=v;
if not used[v] then begin
	used[v]:=true;
	first[v]:=cc;
end;
end;
 
procedure dfs(v:longint);
var
	i,k:longint;
begin
mark(v);	
was[v]:=true;
k:=length(pp[v])-1;
for i:=0 to k do 
	if (not was[pp[v][i]]) then begin
		dfs(pp[v][i]);
		mark(v);
	end;
end;
 
begin
reset(input,'input.txt');
rewrite(output,'output.txt');
readln(q);
cq:=0;
for qq:=1 to q do begin
	readln(s);
	kk:=pos(' ',s);
	t:=copy(s,1,kk-1);
	delete(s,1,kk);
	kk:=pos(' ',s);
	aa:=strtoint(copy(s,1,kk-1));
	delete(s,1,kk);
	bb:=strtoint(s);
	if t='GET' then begin
		inc(cq);
		query[cq][1]:=aa;
		query[cq][2]:=bb;	
	end else begin
		setlength(pp[aa],length(pp[aa])+1);
		pp[aa][length(pp[aa])-1]:=bb;		
	end;
end; 
cc:=0;  
fillchar(was,sizeof(was),0);
fillchar(used,sizeof(used),0);
dfs(1);
k:=build(cc);
for j:=1 to cq do begin
	pra:=first[query[j][1]];
	prb:=first[query[j][2]];
	if pra>prb then swap(pra,prb);
	writeln(b[rmq(pra,prb)]);
end;
end.